package com.sskj.fca.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.fca.R;
import com.sskj.fca.bean.NoticeBean;
import com.sskj.fca.presenter.NoticeCenterListActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.APP_NOTICE_CENTER_LIST)//通知中心列表
public class NoticeCenterListActivity extends BaseActivity<NoticeCenterListActivityPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private ModelRx2DataSource dataSource;
    private MVCCoolHelper<List<NoticeBean>> mvcHelper;
    private SlimAdapter slimAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_notice_center_list;
    }

    @Override
    public NoticeCenterListActivityPresenter getPresenter() {
        return new NoticeCenterListActivityPresenter();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mvcHelper.refresh();
    }

    @Override
    protected void initView() {
        setTitle("通知中心");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_notice_center, new SlimInjector<NoticeBean>() {
            @Override
            public void onInject(NoticeBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_content, data.getContent())
                        .text(R.id.tv_date, data.getCreate_time())
                        .visibility(R.id.iv_notice, data.getIs_read() == 0 ? View.VISIBLE : View.GONE)
                        .clicked(R.id.ll_notice, view -> {
                            ARouter.getInstance().build(RConfig.APP_NOTICE_CENTER_DETAIL)
                                    .withString(Constans.NOTICE_ID, data.getId()).navigation();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<NoticeBean>)
                        page -> mPresenter.getNoticeList(page, 10), 10);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

}
